package com.mioto.ld.module.alert.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.alert.model.*;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.alert.service.ILinkageService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 * @author mioto
 * @date 2024-05-20 23:49:40
 */
@RestController
@RequestMapping("linkage")
@Api(tags = "场景联动配置管理")
public class LinkageController {
    @Resource
    private ILinkageService linkageService;

    @GetMapping
    @ApiOperation(value="查询场景联动配置列表",response = Linkage.class)
    public ResultData list (HttpServletRequest request,Linkage linkage){
        return ResultData.success(linkageService.findList(linkage));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询场景联动配置",response = LinkagePagerVO.class)
    public ResultData pager (HttpServletRequest request, Linkage linkage,BasePager basePager){
        List<LinkagePagerVO> list = new ArrayList<>(0);
        LinkageStatusStatisticVO linkageStatusStatisticVO= linkageService.findCount(linkage);
        if (linkageStatusStatisticVO.getTotalCount() > 0){
            list = linkageService.findPager(linkage,basePager);
        }
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", linkageStatusStatisticVO.getTotalCount());
        result.put("list", list);
        result.put("statistic",linkageStatusStatisticVO);
        return ResultData.success(result);
    }

    @GetMapping("/detail")
    @ApiOperation(value="查询场景联动配置",response = LinkageDetailVO.class)
    public ResultData findDetail (int id){
        return ResultData.success(linkageService.findDetail(id));
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询场景联动配置",response = Linkage.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(linkageService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增场景联动配置")
    public ResultData add (@RequestBody LinkageDTO linkageDTO){
        return Optional.of(linkageService.insert(linkageDTO))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(linkageDTO))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新场景联动配置 局部更新,忽略空值")
    public ResultData updateIgnoreNull (@RequestBody LinkageDTO linkage){
        return Optional.of(linkageService.updateIgnoreNull(linkage))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(linkage))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新场景联动配置 整体更新")
    public ResultData update (Linkage linkage){
        return Optional.of(linkageService.update(linkage))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(linkage))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除场景联动配置")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(linkageService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除场景联动配置")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(linkageService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }
}